package com.ozo.boot.auth.log;

import cn.hutool.core.bean.BeanUtil;
import com.ozo.boot.system.entity.SysLoginLog;
import com.ozo.boot.system.pojo.dto.SysLoginLogDTO;
import com.ozo.boot.system.service.ISysLoginLogService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * 登录事件监听
 *
 * @author qiangesoft
 * @since 2024-07-11
 */
@Slf4j
@RequiredArgsConstructor
@Component
public class LoginLogListener {

    private final ISysLoginLogService sysLoginLogService;

    @Async
    @EventListener({LoginLogEvent.class})
    public void saveSysLog(LoginLogEvent event) {
        SysLoginLogDTO loginLogDTO = (SysLoginLogDTO) event.getSource();
        SysLoginLog sysLoginLog = BeanUtil.copyProperties(loginLogDTO, SysLoginLog.class);
        sysLoginLogService.save(sysLoginLog);
    }

}
